home *** CD-ROM | disk | FTP | other *** search
- unit Convertr;
- { $DEFINE DEBUG}
- interface
-
- uses Classes, SysUtils, NewParse, HtmlTool;
-
- type
- THtmlFileMover = class(THtmlParser)
- protected
- FDestPath: string;
- FSrcPath: string;
- FNoChangeList: TStringList;
- Source, Dest: TStream;
- OutStr: string;
- Line, Position: Integer;
- function CorrectLink(S: String): string; virtual;
- function MakeTagLegal(S: String): string; virtual;
- public
- constructor CreateNew(SSource, SDest: TStream); virtual;
- destructor Destroy; override;
- procedure Convert; virtual;
- property DestPath: string read FDestPath write FDestPath;
- property SrcPath: string read FSrcPath write FSrcPath;
- property NoChangeList: TStringList read FNoChangeList write FNoChangeList;
- end;
-
- THtmlFileCorrector = class(THtmlFileMover)
- protected
- FOldLinks: TStringList;
- function CorrectLink(S: String): string; override;
- public
- constructor CreateNew(SSource, SDest: TStream); override;
- destructor Destroy; override;
- procedure Convert; override;
- property OldLinks: TStringList read FOldLinks write FOldLinks;
- end;
-
- {$IFDEF DEBUG}
- var
- Log1: TextFile;
- {$ENDIF}
-
- implementation
-
- { ******* class THtmlFileMover ******* }
- constructor THtmlFileMover.CreateNew(SSource, SDest: TStream);
- begin
- inherited Create(SSource);
- FNoChangeList := TStringList.Create;
- Source := SSource;
- Dest := SDest;
- SetLength(OutStr, 10000);
- OutStr := '';
- end;
-
- destructor THtmlFileMover.Destroy;
- begin
- FNoChangeList.Free;
- inherited Destroy;
- end;
-
- function THtmlFileMover.CorrectLink(S: string): string;
- var
- AbsPath, NewRelPath: string;
- begin
- Result := '';
- AppendStr(Result, Copy(S, 0, Pos('"', S)));
- // convert link to abs path relative to the source file
- AbsPath := RelToAbsPath(SrcPath, UnixToDosPath(
- Copy(S, Pos('"', S)+1, Length(S))));
- // convert the link to a relative path based on where
- // the file will be after the move
- NewRelPath := AbsToRelPath(DestPath, AbsPath);
- AppendStr(Result, NewRelPath);
- Result := DosToUnixPath(Result);
- end;
-
- function THtmlFileMover.MakeTagLegal(S: String): string;
- begin
- if Token = toOpenTag then Result := '<'
- else Result := '</';
- AppendStr(Result, S);
- AppendStr(Result, '>');
- if Token = toOpenTag then Position := Position + 2
- else Position := Position + 3;
- end;
-
- procedure THtmlFileMover.Convert;
-
- function InList(AName: string): boolean;
- var
- i: integer;
- begin
- Result := false;
- for i := 0 to FNoChangeList.Count-1 do
- if Pos(uppercase(ExtractFileName(FNoChangeList[i])), uppercase(AName)) <> 0 then
- begin
- Result := true;
- Exit;
- end;
- end;
-
- begin
- Line := 1;
- Position := 0;
- // parse the entire source file
- while Token <> toEOF do
- begin
- // if the source code line has changed,
- // add the proper newline character
- while SourceLine > Line do
- begin
- AppendStr(OutStr, #13#10);
- Inc(Line);
- Position := Position + 2; // 2 characters, cr+lf
- end;
- // add proper white spaces (formatting)
- while SourcePos > Position do
- begin
- AppendStr(OutStr, ' ');
- Inc(Position);
- end;
- // check the token
- case Token of
- toSymbol: AppendStr(OutStr, TokenString);
- toInteger: AppendStr(OutStr, TokenString);
- toFloat: AppendStr(OutStr, TokenString);
- toOpenTag: if (((Pos('A HREF="', UpperCase(TokenString)) > 0) or
- (Pos('IMG SRC="', UpperCase(TokenString)) > 0) or
- (Pos('BODY BACKGROUND="', UpperCase(TokenString)) > 0)) and (not
- ((Pos('MAILTO', UpperCase(TokenString)) > 0) or
- (Pos('HTTP', UpperCase(TokenString)) > 0) or
- (Pos('NEWS', UpperCase(TokenString)) > 0) or
- InList(TokenString)))) then
- AppendStr(OutStr, MakeTagLegal(CorrectLink(TokenString)))
- else AppendStr(OutStr, MakeTagLegal(TokenString));
- toCloseTag: AppendStr(OutStr, MakeTagLegal(TokenString));
- else AppendStr(OutStr, Token);
- end; // case Token of
- // increase the current position
- Position := Position + Length(TokenString);
- // move to the next token
- NextToken;
- end;
- // add the string to the stream
- Dest.WriteBuffer(Pointer(OutStr)^, Length(OutStr));
- end;
-
- { ******* class THtmlFileCorrector ******* }
- constructor THtmlFileCorrector.CreateNew(SSource, SDest: TStream);
- begin
- inherited CreateNew(SSource, SDest);
- FOldLinks := TStringList.Create;
- end;
-
- destructor THtmlFileCorrector.Destroy;
- begin
- FOldLinks.Free;
- inherited Destroy;
- end;
-
- function THtmlFileCorrector.CorrectLink(S: string): string;
- var
- AbsPath, NewRelPath: string;
- begin
- Result := '';
- AppendStr(Result, Copy(S, 0, Pos('"', S)));
- // convert link to abs path relative to the source file
- AbsPath := RelToAbsPath(SrcPath, ExtractFileName(
- UnixToDosPath(Copy(S, Pos('"', S)+1, Length(S)))));
- // convert the link to a relative path based on where
- // the file will be after the move
- NewRelPath := AbsToRelPath(DestPath, AbsPath);
- AppendStr(Result, NewRelPath);
- Result := DosToUnixPath(Result);
- end;
-
- procedure THtmlFileCorrector.Convert;
-
- function InList(AName: string): boolean;
- var
- i: integer;
- begin
- Result := false;
- for i := 0 to FOldLinks.Count-1 do
- if Pos(uppercase(ExtractFileName(FOldLinks[i])), uppercase(AName)) <> 0 then
- begin
- Result := true;
- Exit;
- end;
- end;
-
- begin
- Line := 1;
- Position := 0;
- // parse the entire source file
- while Token <> toEOF do
- begin
- // if the source code line has changed,
- // add the proper newline character
- while SourceLine > Line do
- begin
- AppendStr(OutStr, #13#10);
- Inc(Line);
- Position := Position + 2; // 2 characters, cr+lf
- end;
- // add proper white spaces (formatting)
- while SourcePos > Position do
- begin
- AppendStr(OutStr, ' ');
- Inc(Position);
- end;
- // check the token
- case Token of
- toSymbol: AppendStr(OutStr, TokenString);
- toInteger: AppendStr(OutStr, TokenString);
- toFloat: AppendStr(OutStr, TokenString);
- toOpenTag: if InList(TokenString) then
- AppendStr(OutStr, MakeTagLegal(CorrectLink(TokenString)))
- else AppendStr(OutStr, MakeTagLegal(TokenString));
- toCloseTag: AppendStr(OutStr, MakeTagLegal(TokenString));
- else AppendStr(OutStr, Token);
- end; // case Token of
- // increase the current position
- Position := Position + Length(TokenString);
- // move to the next token
- NextToken;
- end;
- // add the string to the stream
- Dest.WriteBuffer(Pointer(OutStr)^, Length(OutStr));
- end;
-
- {$IFDEF DEBUG}
- initialization
- AssignFile(Log1, 'c:\temp\debug1.log');
- Rewrite(Log1);
- finalization
- CloseFile(Log1);
- {$ENDIF}
- end.
-